package org.hk.utils;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@WebServlet("/*")
public class SpringServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doPost(request,response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
        //springmvc就是网络请求过来的路径 做切割后 利用反射完成方法调用
        String url = request.getRequestURI();
        String[] names = url.split("/");
        Object obj = SpringMVCContextUtils.ctx.getBean(names[2]);
        try {
            Method method = obj.getClass().getDeclaredMethod(names[3], null);
            Object returnVal = method.invoke(obj, null);
            out.print(returnVal);
            out.flush();
            out.close();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
